<template>
    <div class="d-flex d-flex-between d-flex-items-center header">
        <img src="@/assets/image/logo-3.png" class="w-15p" @click="toIndex()">

        <div v-if="!user.token">
            <el-button size="small" @click="tologin()">注册</el-button>
            <el-button type="danger" size="small" @click="toreg()">登录</el-button>
        </div>

        <!-- 已登陆 -->
        <div class="rlbox" v-if="user.token">
            <img class="label_1" :src="user.avatar" />

            <div class="phone" v-if="pproshow">{{ user.username }}</div>
            <div class="phone" v-if="proshow">{{ user.compname }}</div>
            <img class="thumbnail_1" referrerpolicy="no-referrer" src="../assets/image/down.png" />

            <ul class="dropdown-menu">
                <!-- 企业 -->
                <!-- <li @click="toProject" v-if="proshow">发布的项目</li> -->
                <!-- 个人 -->
                <!-- <li @click="toPersonProjectList" v-if="pproshow">发布的项目</li> -->

                <li @click="logout">退出登录</li>
            </ul>
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
    data() {
        return {
            rl: true,
            rlsuccess: false,
            pproshow: true,
            proshow: false,
            compinfoShow: false,
        };
    },
    computed: {
        ...mapState(['user']),
    },
    mounted() {
        const userInfoString = localStorage.getItem('rcjj_userinfo');
        if (userInfoString) {
            const userInfo = JSON.parse(userInfoString);
            if (userInfo.token) {
                this.rl = false
                this.rlsuccess = true
                this.avatar = userInfo.avatar
                this.username = userInfo.username
                this.compname = userInfo.comp_name
                this.compinfoShow = false
            }
            if (userInfo.is_comp == 1) { //1个人 2企业
                this.pproshow = true;
                this.proshow = false;
                this.compinfoShow = false;
            } else {
                this.pproshow = false;
                this.proshow = true;
                this.compinfoShow = true;
            }
        }
    },
    methods: {
        // 退出登录
        logout() {
            const userInfoString = localStorage.getItem('rcjj_userinfo');
            if (userInfoString) {
                const userInfo = JSON.parse(userInfoString);
                if (userInfo.is_comp == 1) { //1个人 2企业
                    localStorage.removeItem('rcjj_userinfo')
                    this.$message({ message: '退出登录成功', type: 'success' })
                    this.$store.dispatch('common/init')
                    setTimeout(() => {
                        this.$router.push('/login')
                    }, 1000)
                } else {
                    localStorage.removeItem('rcjj_userinfo')
                    this.$message({ message: '退出登录成功', type: 'success' })
                    this.$store.dispatch('common/init')
                    setTimeout(() => {
                        // this.$router.push('/elogin')
                        this.$router.push('/login')
                    }, 1000)
                }
            }
        },
        tologin() {
            this.$router.push('/login')
        },
        toreg() {
            this.$router.push('/login')
        },
        jump(url) {
            this.$router.push(url)
        },
        toIndex() {
            this.$router.push('/index')
        },

    },
};
</script>

<style lang="scss" scoped>
li {
    list-style: none;
}

.line {
    position: absolute;
    width: 37px;
    height: 5px;
    background-color: #0a42b1;
    // background-color: #fff;
    bottom: -3px;
    left: 50%;
    transform: translateX(-50%);
}

* {
    font-family: PingFangSC, PingFang SC;
    font-style: normal;
}

.headerbox {
    position: fixed;
    top: 0px;
    z-index: 999;
    width: 100%;
    height: 79px;
    background: #fff;
    box-shadow: 18px 1px 18px 1px rgba(169, 156, 156, 0.92);
}

.banner {
    width: 1195px;
    margin: 0 auto;
}

.header {
    height: 79px;
    display: flex;
    align-items: center;
    position: relative;
    justify-content: space-between;
}

.headertit {
    width: 169px;
    height: 35px;
}

.tab {
    display: flex;
    justify-content: space-between;
    position: absolute;
    left: 246px;
}

.erl {
    margin-left: 15px;
    cursor: pointer;
}

.tab .tabs {
    height: 75px;
    font-weight: 600;
    font-size: 18px;
    color: #333333;
    cursor: pointer;
    display: flex;
    align-items: center;
    margin: 0 50px;
    position: relative;
}

// .tabs.active {
//     border-bottom: 5px solid #0a42b1;
//     color: #000000;
// }

// .tabs.active {
//     background-color: #0d5fcd;
// 	color: #fff;
// }

.rlbox {
    display: flex;
    position: absolute;
    right: 0;
    height: 79px;
}

.log {
    width: 80px;
    height: 36px;
    background: #fffcfc;
    border-radius: 4px;
    border: 1px solid #979797;
    font-weight: 600;
    font-size: 18px;
    color: #333333;
    line-height: 36px;
    text-align: center;
    margin-right: 15px;
    cursor: pointer;
}

.reg {
    width: 80px;
    height: 36px;
    background: #093fad;
    border: 1px solid #093fad;
    border-radius: 4px;
    font-weight: 600;
    font-size: 18px;
    color: #ffffff;
    line-height: 36px;
    text-align: center;
    cursor: pointer;
}

.rlbox {
    display: flex;
    font-size: 18px;
    color: #666666;
    font-weight: normal;
    align-items: center;
    position: relative;
}

.rlbox:hover .dropdown-menu {
    display: block;
}

.label_1 {
    width: 32px;
    height: 32px;
    margin-right: 7px;
}

.phone {
    max-width: 200px;
    font-weight: 600;
    font-size: 18px;
    color: #333333;
    text-align: center;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.thumbnail_1 {
    width: 10px;
    height: 6px;
    margin-left: 4px;
}

.dropdown-menu {
    display: none;
    z-index: 999;
    width: 165px;
    position: absolute;
    top: 60px;
    left: 50%;
    transform: translateX(-50%);
    border-radius: 5px;
    overflow: hidden;
    box-sizing: border-box;
    box-shadow: 0 5px 20px 0 hsla(0, 0%, 78.8%, 0.6);
    background-color: #fff;
    cursor: pointer;
    padding: 0;
}

.dropdown-menu li {
    font-size: 14px;
    width: 165px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    background-color: #fff;
    color: #333333;
    border-bottom: 1px solid #eee;
    font-weight: 600;
}

.dropdown-menu li:hover {
    background: #E6F4FF;
    color: #0161FF;
}

/* 媒体查询，针对手机设备 */
@media (max-width: 768px) {
    .header {
        height: 8% !important;
        padding: 0 4% !important;
        width: 91% !important;
    }

    .w-15p {
        width: 35%;
    }

    .phone {
        font-size: 15px;
    }

    .label_1 {
        width: 28px;
        height: 28px;
    }
}
</style>